//
//  AVaudioplayerManager.m
//  蓝牙
//
//  Created by 赵鹏宇 on 16/2/23.
//  Copyright © 2016年 赵鹏宇. All rights reserved.
//

#import "AVaudioplayerManager.h"
#import "RGBManager.h"

@interface AVaudioplayerManager()
{
    double _i;
}
@property (nonatomic , strong) NSTimer * timer;

@end

@implementation AVaudioplayerManager

+(instancetype)standardManager
{
    static AVaudioplayerManager *m = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        m = [[self alloc]init];
    });
    return m;
}

- (void)setupWithSong:(Song *)song delegate:(id<AVAudioPlayerDelegate>)delegate Block:(ErrorBlock)AVAudioPlayerCreateErrorBlock
{
    NSMutableString * s = [NSMutableString stringWithString:song.songTitle];
    [s appendString:song.songArtist];
    [s appendString:song.songAlbum];
    //保存播放歌曲信息
    [[NSUserDefaults standardUserDefaults] setValue:s forKey:@"isSelect"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    NSError *error;

    self.player = [[AVAudioPlayer alloc]initWithContentsOfURL:song.songURL error:&error];
    self.player.delegate = delegate;
    if (error) {
        if (AVAudioPlayerCreateErrorBlock) {
            AVAudioPlayerCreateErrorBlock(error);
        }
    } else {
        self.playingSong = song;
        [self.player prepareToPlay];
        //获取歌曲总时间
        song.sumTime = self.player.duration;
        if (AVAudioPlayerCreateErrorBlock) {
            AVAudioPlayerCreateErrorBlock(nil);
        }
    }
}
- (void)doWork
{
    
}

- (void)playMusic
{
    [self.player play];
    
    [RGBManager standardManager];
    BOOL rhythmMode = [[NSUserDefaults standardUserDefaults] boolForKey:kRhythmMode];
    if (rhythmMode) {
        [RGBManager start];
    } else {
        [RGBManager stop];
    }
}

- (void)stopPlay
{
    [self.player stop];
    [RGBManager stop];
}

- (void)pausePlay
{
    [self.player pause];
    [RGBManager stop];
}

@end
